import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() => runApp(const MediaQueryFeatureWidget());

GlobalKey _globalKey = GlobalKey();

class MediaQueryFeatureWidget extends StatelessWidget {
  const MediaQueryFeatureWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Flutter Demo",
      theme: ThemeData(
        primaryColor: Colors.blue,
      ),
      home: Scaffold(
          appBar: AppBar(title: const Text("Flutter 功能类Widget -- MediaQuery")),
          body: const BodyWidget()),
    );
  }
}

class BodyWidget extends StatelessWidget {
  const BodyWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Text(
          "Hello Flutter",
          key: _globalKey,
        ),
        ElevatedButton(
          child: const Text('getSize'),
          onPressed: () {
            //获取屏幕的宽高
            if (kDebugMode) {
              print("Screen width:" +
                  MediaQuery.of(context).size.width.toString() +
                  " Screen height:" +
                  MediaQuery.of(context).size.height.toString());
            }
            //获取子Widget 的宽高
            if (kDebugMode) {
              print("Ttext width:" +
                  (_globalKey.currentContext?.size?.width.toString() ?? "") +
                  " Screen height:" +
                  (_globalKey.currentContext?.size?.height.toString() ?? ""));
            }
          },
        )
      ],
    );
  }
}
